package addressBook;

import java.awt.event.*;

public class searchWindowController implements ActionListener {
	
	private searchWindowView theView;
	
	public searchWindowController(searchWindowView theView, searchWindowModel theModel)
	{
		this.theView = theView;
		
		this.theView.addButtonListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String[] contact = new String[7];
		contact c = new contact(contact);
		boolean search = false;
		
		// Surround interactions with the view with
		// a try block in case field(s) weren't
		// properly entered
		
		try{
		
			c.setFirstName(theView.getFirstName());
			c.setLastName(theView.getLastName());
			c.setPersoHomeNum(theView.getPhoneNumber());
			c.setAddress(theView.getAddress());
			c.setCity(theView.getCity());
			c.setUrl(theView.getUrl());
			c.setEmail(theView.getEmail());
			
			if(c.getFirstName().isEmpty() || c.getLastName().isEmpty() || c.getCity().isEmpty())
				throw new IllegalArgumentException("Enter at least First Name, Last Name and City!");
			else
				search = theView.myContacts.searchContact(c);
				if(search==true)
					throw new IllegalArgumentException("The contact exists!");
				else
					throw new IllegalArgumentException("The contact doesn't exist!");
				
		}

		catch(IllegalArgumentException ex){
			
			theView.displayErrorMessage(ex);
			if(ex.getMessage().equals("The contact exists!"))
				theView.setVisible(false);
			
		}
		
	}
}
